package com.zzyl.nursing.service.impl;

import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.BaseEntity;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.vo.health.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PutMapping;

/**
 * 预约信息Service业务层处理
 * 
 * @author zy
 * @date 2025-08-19
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {

        reservation.setCreateBy(String.valueOf(UserThreadLocal.getUserId()));
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<TimeCountVo> getCountByTime(Long time) {
        //根据传递过来的时间获取到当天的开始时间和结束时间
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(localDateTime);
        LocalDateTime endTime = LocalDateTimeUtil.endOfDay(localDateTime);

        List<TimeCountVo> list=reservationMapper.getCountByTime(startTime,endTime);

        return list;
    }



    @Override
    public TableDataInfo<Reservation> getpage(Integer pageSize, Integer pageNum,Integer status) {

        Page<Reservation> page = Page.of(pageNum, pageSize);


        Long userId = UserThreadLocal.getUserId();
        page=page(page, Wrappers.<Reservation>lambdaQuery()
                .eq(status!=null&& !"".equals(status),Reservation::getStatus,status)
                .eq(Reservation::getCreateBy, userId));


        TableDataInfo tableDataInfo = new TableDataInfo();

        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("操作成功");
        return tableDataInfo;
    }


    /**
     * 定时更新预约状态
     */
    @Override
    public void updateReservation() {
        //1.查找出所有状态为0且time的时间在当前之间后面 的集合
        LocalDateTime now = LocalDateTime.now();
        List<Reservation> list = reservationMapper.selectList(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, now));

        //2.将这些集合中reservation对象的状态更改为3(过期)
        for (Reservation reservation : list) {
            reservation.setStatus(3);
            reservation.setUpdateBy("1");
            reservation.setUpdateTime(new Date());
        }

        //3.批量保存集合对象
        updateBatchById(list);
    }


}
